package com.example.logining.handler;

import cn.hutool.json.JSONUtil;
import com.example.logining.domain.result.Result;
import com.example.logining.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @title: AccessDeniedHandlerImpl
 * @author: wywang
 * @date: 2024/11/17
 * @description: 自定义授权异常处理类
 **/
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException,
            ServletException {
        Result<String> result = Result.error("权限不足", 403);
        String jsonStr = JSONUtil.toJsonStr(result);

        WebUtils.renderString(response, jsonStr);
    }
}
